package com.campus.secondhand.controller;

import java.math.BigDecimal;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.campus.secondhand.common.Result;
import com.campus.secondhand.model.dto.SearchResultDTO;
import com.campus.secondhand.service.SearchService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 搜索控制器
 */
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
@Tag(name = "搜索接口")
public class SearchController {

    private final SearchService searchService;

    /**
     * 综合搜索
     */
    @GetMapping
    @Operation(summary = "综合搜索")
    public Result<SearchResultDTO> search(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "页码") @RequestParam(required = false) Integer page,
            @Parameter(description = "每页大小") @RequestParam(required = false) Integer pageSize,
            @Parameter(description = "排序方式") @RequestParam(required = false) String sortBy) {
        return searchService.search(keyword, page, pageSize, sortBy);
    }

    /**
     * 搜索商品
     */
    @GetMapping("/products")
    @Operation(summary = "搜索商品")
    public Result<Map<String, Object>> searchProducts(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "页码") @RequestParam(required = false) Integer page,
            @Parameter(description = "每页大小") @RequestParam(required = false) Integer pageSize,
            @Parameter(description = "排序方式") @RequestParam(required = false) String sortBy,
            @Parameter(description = "分类ID") @RequestParam(required = false) Long categoryId,
            @Parameter(description = "学校ID") @RequestParam(required = false) Long schoolId,
            @Parameter(description = "最低价格") @RequestParam(required = false) BigDecimal minPrice,
            @Parameter(description = "最高价格") @RequestParam(required = false) BigDecimal maxPrice) {
        return searchService.searchProducts(keyword, page, pageSize, sortBy, categoryId, schoolId, minPrice, maxPrice);
    }

    /**
     * 搜索学校
     */
    @GetMapping("/schools")
    @Operation(summary = "搜索学校")
    public Result<Map<String, Object>> searchSchools(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "页码") @RequestParam(required = false) Integer page,
            @Parameter(description = "每页大小") @RequestParam(required = false) Integer pageSize) {
        return searchService.searchSchools(keyword, page, pageSize);
    }

    /**
     * 搜索用户
     */
    @GetMapping("/users")
    @Operation(summary = "搜索用户")
    public Result<Map<String, Object>> searchUsers(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "页码") @RequestParam(required = false) Integer page,
            @Parameter(description = "每页大小") @RequestParam(required = false) Integer pageSize,
            @Parameter(description = "学校ID") @RequestParam(required = false) Long schoolId) {
        return searchService.searchUsers(keyword, page, pageSize, schoolId);
    }

    /**
     * 将所有商品同步到ES
     */
    @PostMapping("/sync")
    @Operation(summary = "同步所有商品到ES")
    public Result<String> syncToEs() {
        return searchService.syncAllProductsToEs();
    }
}